package com.example.administrator.yuejian_2.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.TrainListAdapter;
import com.example.administrator.yuejian_2.bean.TrainListBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.TrainService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class TrainNoobFragment extends Fragment {


    private RecyclerView recyclerView;
    private TrainListAdapter adapter;

    public TrainNoobFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView=view.findViewById(R.id.train_noob_recyclerView);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        TrainService service = retrofit.create(TrainService.class);
        Call<List<TrainListBean>> call = service.coursenooblist();
        call.enqueue(new Callback<List<TrainListBean>>() {
            @Override
            public void onResponse(Call<List<TrainListBean>> call, Response<List<TrainListBean>> response) {
                if (getActivity() == null)
                    return;
                adapter = new TrainListAdapter(getActivity(),response.body());//adapter显示一条数据
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());//定义布局
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//定义布局
                recyclerView.setLayoutManager(layoutManager);//
                recyclerView.setAdapter(adapter);//调用适配器
                recyclerView.setItemAnimator(new DefaultItemAnimator());//调用item
            }

            @Override
            public void onFailure(Call<List<TrainListBean>> call, Throwable t) {

            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_train_noob, container, false);
    }



}

